$('.table').hide();
get_dns_addr = function () {
    $.ajax({
        type: "GET",
        url: 'https://api.viabk.com/tools/get_dns_addr',
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            if (msg.success != 1) {
                return false;
            } else {
                data = msg.data;
                count_list = [];
                for (i in data) {
                    addr_list = data[i];
                    id = addr_list.id;
                    addr = addr_list.addr;
                    tr = "<tr num=" + id + "><td>" + addr + "</td><td>loading...</td><td>loading...</td></tr>";
                    $('.append-tr').append(tr);
                }
            }
        }

    })
}
get_dns_addr();

$('.select-btn1').click(function () {
    if ($('.select-img-d').attr('src') == 'static/down.png') {
        $('.select-img-d').attr('src', 'static/up.png');
        $('.select-hide').show();
    } else {
        $('.select-img-d').attr('src', 'static/down.png');
        $('.select-hide').hide();
    }
})
$('.select-li').mouseenter(function () {
    $(this).css('background', "#ccc");
})
$('.select-li').mouseleave(function () {
    $(this).css('background', "#fff");
})
$('.select-li').click(function () {
    $('.select-img-d').attr('src', 'static/down.png');
    $('.select-hide').hide();
    $('.select-ready').html($(this).html());
})
$('.select-btn').click(function () {
    dns_type = $('.select-ready').html();
    domain_name = $('.select-2').val();
    var reg = new RegExp("([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}");
    if (reg.test(domain_name)) {
        num_list = new Array();
        $('.table').show();
        $('tr[num]').each(function (index) {
            num_list.push($(this).attr("num"));
            $('tr[num] td:nth-child(2)').html('loading...');
            $('tr[num] td:nth-child(3)').html('');
        })
        $('tr[num]').each(function () {
            var obj = $(this);
            opt = {
                "dns_id": num_list[i],
                "domain_name": domain_name,
                "dns_type": dns_type
            }
            $.ajax({
                type: "POST",
                url: 'https://api.viabk.com/tools/select_DNS',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(opt),
                success: function (msg) {
                    if (msg.success != 1) {
                        return false;
                    } else {
                        IPs = msg.data.IPs;
                        ttls = msg.data.ttls;
                        num = opt.dns_id;
                        obj.find('td:eq(1)').html(IPs);
                        obj.find('td:eq(2)').html(ttls);
                    }
                }
            })
        })

        $('[num]').mouseenter(function () {
            $(this).attr({'class': 'tr-background'});
        })
        $('[num]').mouseleave(function () {
            $(this).removeAttr('class');
        })
    } else {
        confirm("域名填写错误！");
    }
})